package client;

/**
 * Made by Jonas
 */

import javax.swing.*;

import model.exceptions.*;
import net.miginfocom.swing.MigLayout;
import java.awt.*;
import javax.swing.*;


import java.awt.*;
import java.awt.event.*;
import java.rmi.RemoteException;

public class LoginFrame extends JFrame implements ActionListener {

	private JPanel contentPane;
	private TextField loginUserField, createUserField;
	private JPasswordField loginPasswordField, createPasswordField_new_1,
			createPasswordField_new_2;
	private Button createButton, loginButton;
	private ClientController controller;

	/*
	 * Create the frame.
	 */
	public LoginFrame(ClientController controller) {
		super("Log in / Register");
		setVisible(true);
		this.controller = controller;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(1, 1, 1, 1);
		contentPane = new JPanel();
		contentPane.setBorder(null);
		setContentPane(contentPane);
		contentPane.setLayout(new MigLayout("", "[grow][][grow]",
				"[][][][][][][][]"));

		Panel panel = new Panel();
		panel.setMinimumSize(new Dimension(3, 0));
		panel.setPreferredSize(new Dimension(3, 0));
		panel.setBackground(Color.BLACK);
		panel.setForeground(Color.BLACK);
		contentPane.add(panel, "cell 1 0 1 8,grow");

		Label createUserLabel = new Label("Create user");
		createUserLabel.setFont(new Font("Comic Sans MS", Font.PLAIN, 13));
		createUserLabel.setForeground(Color.RED);
		contentPane.add(createUserLabel, "cell 2 0,alignx center");

		Label newUserLabel = new Label("New user name");
		newUserLabel.setFont(new Font("Comic Sans MS", Font.PLAIN, 13));
		newUserLabel.setForeground(Color.RED);
		contentPane.add(newUserLabel, "cell 2 1,alignx center");

		Label userNameLabel = new Label("User name");
		userNameLabel.setFont(new Font("Comic Sans MS", Font.PLAIN, 13));
		userNameLabel.setAlignment(Label.CENTER);
		userNameLabel.setPreferredSize(new Dimension(150, 0));
		contentPane.add(userNameLabel, "flowx,cell 0 2,alignx center");

		createUserField = new TextField();
		createUserField.setForeground(Color.RED);
		createUserField.setPreferredSize(new Dimension(150, 0));
		contentPane.add(createUserField, "cell 2 2,alignx center");

		loginUserField = new TextField();
		loginUserField.setPreferredSize(new Dimension(150, 0));
		contentPane.add(loginUserField, "cell 0 3,alignx center");

		Label newPassLabel_1 = new Label("Enter password");
		newPassLabel_1.setFont(new Font("Comic Sans MS", Font.PLAIN, 13));
		newPassLabel_1.setForeground(Color.RED);
		contentPane.add(newPassLabel_1, "cell 2 3,alignx center");

		Label PassLabel = new Label("Password");
		PassLabel.setFont(new Font("Comic Sans MS", Font.PLAIN, 13));
		contentPane.add(PassLabel, "cell 0 4,alignx center");

		createPasswordField_new_1 = new JPasswordField();
		createPasswordField_new_1.setForeground(Color.RED);
		createPasswordField_new_1.setPreferredSize(new Dimension(150, 20));
		contentPane.add(createPasswordField_new_1, "cell 2 4,alignx center");

		loginPasswordField = new JPasswordField();
		loginPasswordField.setPreferredSize(new Dimension(150, 20));
		contentPane.add(loginPasswordField, "cell 0 5,alignx center");

		Label newPassLabel_2 = new Label("Repeat password");
		newPassLabel_2.setFont(new Font("Comic Sans MS", Font.PLAIN, 13));
		newPassLabel_2.setForeground(Color.RED);
		contentPane.add(newPassLabel_2, "cell 2 5,alignx center");

		Label label_1 = new Label("Login user");
		  label_1.setPreferredSize(new Dimension(150, 0));
		  label_1.setFont(new Font("Comic Sans MS", Font.PLAIN, 13));
		  label_1.setAlignment(Label.CENTER);
		  contentPane.add(label_1, "cell 0 0,alignx center");
		
		createPasswordField_new_2 = new JPasswordField();
		createPasswordField_new_2.setForeground(Color.RED);
		createPasswordField_new_2.setPreferredSize(new Dimension(150, 20));
		contentPane.add(createPasswordField_new_2, "cell 2 6,alignx center");

		loginButton = new Button("Login");
		loginButton.setFont(new Font("Comic Sans MS", Font.PLAIN, 13));
		loginButton.addActionListener(this);
		contentPane.add(loginButton, "cell 0 7,alignx center");

		createButton = new Button("Create User and login");
		createButton.setFont(new Font("Comic Sans MS", Font.PLAIN, 13));
		createButton.setForeground(Color.RED);
		createButton.addActionListener(this);
		contentPane.add(createButton, "cell 2 7,alignx center");
		repaint();
		setBounds(100, 100, 800, 275);
		
		setDefaultCloseOperation( DO_NOTHING_ON_CLOSE );
		addWindowListener( new FrameHandler() );
	}
	
	private class FrameHandler extends WindowAdapter {
		public void windowClosing( WindowEvent event ) {
			controller.loginFrameClose();
		}
	}

	public void actionPerformed(ActionEvent ae) {
		if (ae.getSource() == loginButton)
			loginButtonPressed();
		if (ae.getSource() == createButton)
			registerButtonPressed();
	}

	private void loginButtonPressed() {
		try {
			controller.loginPressed(loginUserField.getText(),
					String.valueOf(loginPasswordField.getPassword()));
			this.setVisible(false);
		} catch (AbstractException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.getText());
		}
	}

	private void registerButtonPressed() {
		try {
			controller.registerPressed(createUserField.getText(),
					String.valueOf(createPasswordField_new_1.getPassword()),
					String.valueOf(createPasswordField_new_2.getPassword()));
			this.setVisible(false);
		} catch (AbstractException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null,e.getText());
		}
	}
}
